
/*
7 avr. 2012 jblanchard 
*/

package org.jcb.control.service;

import java.util.ArrayList;
import java.util.List;

import org.jcb.control.dto.ChineseQuestionnaire;
import org.jcb.control.dto.DataChineseLanguage;


public class GenerateQuestionnaireRandomStrategy {

public static ChineseQuestionnaire generate(List<DataChineseLanguage> base, String filter, int nbQuestions) {
	ChineseQuestionnaire cq = new ChineseQuestionnaire();
	List<DataChineseLanguage> data = extractData(base,filter);
	if (data.size() < nbQuestions ) {
		throw new RuntimeException("Trop de question");
	}
	for (int i = 0 ; i < nbQuestions ; i++ ) {
		int random = (int) (Math.random() *  data.size()) ;
		System.out.println("random=" + random);
		cq.addQuestion(data.get(random)) ;
		data.remove(random);
	}
	return cq ;
}

protected static List<DataChineseLanguage> extractData(List<DataChineseLanguage> data,String filter) {
	List<DataChineseLanguage> extract = new ArrayList<>();
	for (DataChineseLanguage dcl : data) {
		if (filter.equals(dcl.getTagQuestion())) {
			extract.add(dcl);
		}
	}
	return extract ;
}


}
